//
//  IVBrickerViewController.m
//  IVBricker
//
//  Created by 葛 剑峰 on 12-6-7.
//  Copyright (c) 2012年 Dream and Talent. All rights reserved.
//

#import "IVBrickerViewController.h"

@interface IVBrickerViewController ()
{
    int         _score;
    float       _touchOffset;
    CGPoint     _ballMovement;
}

-(void)initializeTimer;
-(void)animateBall:(NSTimer*)aTimer;

@end

@implementation IVBrickerViewController

@synthesize scoreLabel = _scoreLabel;
@synthesize ball = _ball;
@synthesize paddle = _paddle;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    _ballMovement = CGPointMake(4, 4);
    
    [self initializeTimer];
}

- (void)viewDidUnload
{
    [self setScoreLabel:nil];
    [self setBall:nil];
    [self setPaddle:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark -
#pragma mark Game Logic Implemenation

-(void)initializeTimer
{
    float interval = 1.0 / 30.0;
    [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(animateBall:) userInfo:nil repeats:YES];
}

-(void)animateBall:(NSTimer *)aTimer
{
    self.ball.center = CGPointMake(self.ball.center.x + _ballMovement.x, self.ball.center.y + _ballMovement.y);
    
    BOOL paddleCollision = (self.ball.center.y >= (self.paddle.center.y - 16)) &&
                           (self.ball.center.y <= (self.paddle.center.y + 16)) &&
                           (self.ball.center.x >= (self.paddle.center.x - 32)) &&
                           (self.ball.center.x <= (self.paddle.center.x + 32));
    
    if (paddleCollision) {
        _ballMovement.y = -_ballMovement.y;
        
        if (self.ball.center.y >= (self.paddle.center.y - 16) && _ballMovement.y < 0) {
            self.ball.center = CGPointMake(self.ball.center.x, self.paddle.center.y - 16);
        }
        else if (self.ball.center.y <= (self.paddle.center.y + 16) && _ballMovement.y > 0) {
            self.ball.center = CGPointMake(self.ball.center.x, self.paddle.center.y + 16);
        }
        else if (self.ball.center.x >= (self.paddle.center.x - 32) && _ballMovement.x < 0) {
            self.ball.center = CGPointMake(self.paddle.center.x - 32, self.ball.center.y);
        }
        else if (self.ball.center.x <= (self.paddle.center.x + 32) && _ballMovement.x > 0) {
            self.ball.center = CGPointMake(self.paddle.center.x + 32, self.ball.center.y);
        }
    }
    
    
    if (self.ball.center.x > 300 || self.ball.center.x < 20) {
        _ballMovement.x = -_ballMovement.x;
    }
    if (self.ball.center.y > 440 || self.ball.center.y < 40) {
        _ballMovement.y = -_ballMovement.y;
    }
}

#pragma mark -
#pragma mark Touch Event Implemenation

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    _touchOffset = self.paddle.center.x - [touch locationInView:touch.view].x;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    float distanceMoved = [touch locationInView:touch.view].x + _touchOffset - self.paddle.center.x;
    float newX = self.paddle.center.x + distanceMoved;
    
    if (newX > 30 && newX < 290) {
        self.paddle.center = CGPointMake(newX, self.paddle.center.y);
    }
    if (newX < 30) {
        self.paddle.center = CGPointMake(30, self.paddle.center.y);
    }
    if (newX > 290) {
        self.paddle.center = CGPointMake(290, self.paddle.center.y);
    }
}

@end
